////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// The MIT License (MIT)                                                      //
//                                                                            //
// Copyright (c) 2013 Charles Teague                                          //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web.SessionState;


using RPSDAL;
using RPSDAL.CollectionClasses;
using RPSDAL.EntityClasses;
using RPSDAL.HelperClasses;

using SD.LLBLGen.Pro.ORMSupportClasses;

/// <summary>
/// Summary description for Parameters
/// </summary>
public class Parameters
{
    public const int TEXTBOX_TYPE = 0;
    public const int COMBOBOBOX_TYPE = 1;
    public const int CHECKBOX_TYPE = 2;
    public const int DATETIME_TYPE = 3;
    public const int TEXTAREA_TYPE = 4;

    public const int CALL_TYPE = 0;
    public const int LETTER_TYPE = 1;
    public const int MAIL_TYPE = 2;

    public static int PageSize
    {

        get
        {
            int PSize;
            if (!int.TryParse(ConfigurationManager.AppSettings["PageRows"], out PSize))
            {
                return 10;
            }
            else
            {
                return PSize;
            }
        }

        //set { ViewState["pageIndex"] = value; }

    }
    
    public static int LoggedUser 
    {
        get {
            int userId = 0;
            if (Session["userId"]!= null && Int32.TryParse(Session["userId"].ToString(), out userId))
            {
                return userId;
            }

            return -1;
        }
        set { Session["userId"] = value; }
    }

    private static HttpSessionState Session
    {
        get
        {
            return HttpContext.Current.Session;
        }
    }

    private static string GetSessionStringValue(string name)
    {
        return Session[name] == null ? null : Session[name].ToString();
    }

    private static void SetSessionValue(string name, object value)
    {
        if (value == null)
        {
            Session.Remove(name);
        }
        else
        {
            Session[name] = value;
        }
    }
}

public class Methods
{
    public static string AppRoot
    {
        get
        {
            if (HttpContext.Current != null)
            {
                string appRoot = HttpContext.Current.Request.PhysicalApplicationPath;
                return (appRoot == "/") ? String.Empty : appRoot;
            }
            else
            {
                return String.Empty;
            }
        }
    }



    public static string auxLetter(string virtualPath,string id, string extension) 
    {
        return virtualPath + "\\letter"+id+"."+extension;
    }

    // upload file
    public static string UploadFile(HttpPostedFile fileUpload, string mapPath)
    {

        //upload file
        string strFilePath = mapPath;
        if (!Directory.Exists(strFilePath))
        {
            Directory.CreateDirectory(strFilePath);
        }

        ContactFileCollection cContactFiles = new ContactFileCollection();
        cContactFiles.GetMulti(null, 0, new SortExpression(new SortClause(ContactFileFields.ContactFileId, SortOperator.Descending)));

        int fileNumber = 1;
        if (cContactFiles.Count > 0)
        {
            fileNumber = cContactFiles[0].ContactFileId + 1;
        }

        string[] fileParts = fileUpload.FileName.Split('.');
        string fileType = fileParts[fileParts.Length - 1];
        string strFile = strFilePath + "\\file" + fileNumber + "." + fileType;// +"\\" + fileUpload.FileName;//fileUpload.PostedFile.FileName
        //upload the file
        if (fileUpload.FileName != "")//is not empty
        {
            fileUpload.SaveAs(strFile);
        }

        return strFile;
    }
}